import json
import xlrd
from django.shortcuts import render
from django.http import HttpResponseRedirect
from userapp.views import get_user_cls
from app.job_infterface import job_rec_cls
from userapp.models import *
from app.models import *

def HomeCls(request):
    res_data = {
        "status": 1,
        "data": {}
    }
    userId = request.GET.get("userId")
    user_dict = get_user_cls(userId)
    res_data["user"] = user_dict
    return render(request, 'home.html', res_data)

def HeatCls(request):
    res_data = {
        "status": 1,
        "data": {}
    }
    userId = request.GET.get("userId")
    user_dict = get_user_cls(userId)
    res_data["user"] = user_dict
    op = request.GET.get("op")
    job_dict = {}
    if op == "one" or not op:
        job_tab = JobTab.objects.filter(year__lt='2024')
        for data in job_tab:
            unit = data.unit
            recruitNum = data.recruitNum
            enrollNum = data.enrollNum
            payNum = data.payNum
            if unit not in job_dict:
                job_dict[unit] = {
                    "recruitNum": recruitNum,
                    "enrollNum": enrollNum,
                    "payNum": payNum
                }
            else:
                job_dict[unit]["recruitNum"] = job_dict[unit]["recruitNum"] + recruitNum
                job_dict[unit]["enrollNum"] = job_dict[unit]["enrollNum"] + enrollNum
                job_dict[unit]["payNum"] = job_dict[unit]["enrollNum"] + payNum
        job_list = []
        for unit, data in job_dict.items():
            job_list.append([data["recruitNum"], data["enrollNum"], data["payNum"], unit])
        job_list.sort()
        top_list = []
        bottom_list = []
        num = 1
        for recruitNum,enrollNum,payNum,unit in job_list[-20:]:
            top_list.append({
                "num": num,
                "unit": unit,
                "recruitNum": recruitNum,
                "enrollNum": enrollNum,
                "payNum": payNum
            })
            num = num + 1
        res_data["data"]["topList"] = top_list
        num = 1
        for recruitNum,enrollNum,payNum,unit in job_list[0:20]:
            bottom_list.append({
                "num": num,
                "unit": unit,
                "recruitNum": recruitNum,
                "enrollNum": enrollNum,
                "payNum": payNum
            })
            num = num + 1
        res_data["data"]["bottomList"] = bottom_list

        return render(request, 'heatOne.html', res_data)
    elif op == "two":
        job_tab = JobTab.objects.filter(year__lt='2024')
        for data in job_tab:
            jobId = data.id
            recruitNum = data.recruitNum
            if jobId not in job_dict:
                job_dict[jobId] = {
                    "recruitNum": recruitNum,
                    "unitNum": 1
                }
            else:
                job_dict[unit]["recruitNum"] = job_dict[jobId]["recruitNum"] + recruitNum
                job_dict[unit]["unitNum"] = job_dict[jobId]["unitNum"] + 1
        name_dict = {}
        major_tab = MajorTab.objects.all()
        for data in major_tab:
            name_dict[data.id] = data.major
        job_major_tab = JobMajorTab.objects.all()
        major_dict = {}
        for data in job_major_tab:
            jobId = data.job_id
            majorId = data.major_id
            major_dict[jobId] = majorId
        major_list = []
        data_dict = {}
        for jobId, data in job_dict.items():
            if jobId not in major_dict:
                continue
            majorId = major_dict[jobId]
            major = name_dict[majorId]
            if major not in data_dict:
                data_dict[major] = {
                    "recruitNum": data["recruitNum"],
                    "unitNum":data["unitNum"]
                }
            else:
                data_dict[major]["recruitNum"] = data_dict[major]["recruitNum"] + data["recruitNum"]
                data_dict[major]["unitNum"] = data_dict[major]["unitNum"] + data["unitNum"]
        for major, data in data_dict.items():
            major_list.append([data["recruitNum"], data["unitNum"], major])
        major_list.sort()
        top_list = []
        bottom_list = []
        num = 1
        for recruitNum, unitNum, major in major_list[-20:]:
            top_list.append({
                "num": num,
                "major": major,
                "jobNum": recruitNum,
                "unitNum": unitNum
            })
            num = num + 1
        res_data["data"]["topList"] = top_list
        num = 1
        for recruitNum, unitNum, major in major_list[0:20]:
            bottom_list.append({
                "num": num,
                "major": major,
                "jobNum": recruitNum,
                "unitNum": unitNum
            })
            num = num + 1
        res_data["data"]["bottomList"] = bottom_list
        return render(request, 'heatTwo.html', res_data)
    return render(request, 'heatOne.html', res_data)

def read_xls_cls(filename):
    data = xlrd.open_workbook(filename)
    sheets = data.sheets()
    data_list = []
    for sheet in sheets:
        lines = []
        name = sheet.name
        for r in range(sheet.nrows):
            col = []
            for c in range(sheet.ncols):
                col.append(sheet.cell(r, c).value)
            lines.append(col)
        data_list.append(lines)
    return data_list

def JobCls(request):
    res_data = {
        "status": 1,
        "data": {}
    }
    userId = request.GET.get("userId")
    user_dict = get_user_cls(userId)
    res_data["user"] = user_dict
    op = request.GET.get("op")
    job_dict = {}
    lines = read_xls_cls("data/2024.xlsx")[0][1:]
    job_list = []
    if op == "one" or not op:
        for line in lines:
            job_list.append({
                "num": int(line[0]),
                "jobCode": line[1],
                "city": line[2],
                "unit": line[3],
                "position": line[4],
                "recruitNum": int(line[5]),
                "education": line[6],
                "major": line[8]
            })
        res_data["data"]["jobList"] = job_list
        return render(request, 'jobOne.html', res_data)
    elif op == "two":
        user_major = user_dict["major"]
        user_education = user_dict["education"]
        all_majors = []
        for line in lines:
            num = int(line[0])
            job_dict[num] = {
                "num": int(line[0]),
                "jobCode": line[1],
                "city": line[2],
                "unit": line[3],
                "position": line[4],
                "recruitNum": int(line[5]),
                "education": line[6],
                "major": line[8]
            }
            education = line[6]
            major = line[8]
            if major:
                major = major.split("、")
                if user_education == "专科" and education == "专科":
                    all_majors.append([num, major])
                elif user_education == "本科" and education in ["专科", "本科"]:
                    all_majors.append([num, major])
                elif user_education == "研究生" and education == "研究生":
                    all_majors.append([num, major])
        tj_job_list = job_rec_cls(user_major, all_majors)
        for seq,num in tj_job_list:
            job_list.append(job_dict[num])
        res_data["data"]["jobList"] = job_list
        return render(request, 'jobTwo.html', res_data)
    return render(request, 'jobOne.html', res_data)